#include "thread_pool.h"
#include <mysql/mysql.h>
#include <stdio.h>
#include "timer.h"

extern MYSQL* conn;
extern User users;
extern Time_Queue* queue;

void loginCommand1(task_t * task){
    char query[1024] = {0};
    char username[64] = {0};
    strncpy(username, task->data, sizeof(username));
    snprintf(query, sizeof(query),
            "SELECT salt FROM t_user WHERE username='%s'", username);
    int ret = mysql_query(conn, query);
    if(ret){
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    MYSQL_RES *res = mysql_store_result(conn);
    int rows = mysql_num_rows(res);
    if(rows == 0){
       sendMsg(task->peerfd, MSG_TYPE_NOT_EXIST, "\0"); 
    }else{
        MYSQL_ROW row = mysql_fetch_row(res);
        if(row != NULL){
            sendMsg(task->peerfd, MSG_TYPE_SUCCESS, row[0]);
        } else{
            sendMsg(task->peerfd, MSG_TYPE_FAILURE, "\0");
        }

    }

}

void loginCommand2(task_t * task){
    char* token = strtok(task->data,":");
    char username[64] = {0};
    strcpy(username, token);
    token = strtok(NULL, ":");
    char passwd[99] = {0};
    strcpy(passwd, token);

    printf("username: %s\n", username);
    printf("passwd: %s\n", passwd);

    char query[1024] = {0};

    sprintf(query,
            "SELECT userid FROM t_user WHERE username='%s' AND password ='%s'",
            username, passwd);

    int ret = mysql_query(conn, query);
    if(ret){
        fprintf(stderr, "%s\n", mysql_error(conn));
    }

    MYSQL_RES *res = mysql_store_result(conn);
    int rows = mysql_num_rows(res);
    if(rows == 0){
        sendMsg(task->peerfd, MSG_TYPE_WRONG_PASSWORD, "\0");
    } else{
        //修改当前目录
        memset(query, 0, sizeof(query));
        sprintf(query,
                "UPDATE t_user SET pwd='/',pwdid=0 WHERE username='%s'",
                username);

        ret = mysql_query(conn, query);
        if(ret){
            fprintf(stderr, "%s\n", mysql_error(conn));
        }

        MYSQL_ROW row = mysql_fetch_row(res);
        
        //把userid发过去
        sendMsg(task->peerfd, MSG_TYPE_SUCCESS, row[0]);

        //将用户添加进用户列表中
        addUser(task->peerfd, atoi(row[0])); 

        // 超时循环队列添加数据
        insert_node(queue, atoi(row[0]) ,task->peerfd);
    }
}


void loginCommand(task_t * task)
{
    char* token = strtok(task->data, ":");
    char buf_name[128] = {0};
    strcpy(buf_name, token);
    printf("buf_name:%s\n", buf_name);
    char buf_pwd[128] = {0};
    token = strtok(NULL, ":");
    strcpy(buf_pwd, token);
    printf("buf_pwd:%s\n", buf_pwd);

    struct spwd *sp;
    char salt[512] = {0};
    int one = 0;

    if((sp=getspnam(buf_name)) == NULL){
        perror("获取用户名和密码");
        sendn(task->peerfd, &one, sizeof(one));
        return;
    }
    //取出 salt,i 记录密码字符下标,j 记录$出现次数
    int i,j;
    for(i=0,j=0;sp->sp_pwdp && j != 4;++i) {
        if(sp->sp_pwdp[i] == '$') ++j;
    }
    strncpy(salt, sp->sp_pwdp, i - 1);

    if(strcmp(sp->sp_pwdp, crypt(buf_pwd, salt)) == 0){
        printf("验证通过\n");
        one = 1;
        sendn(task->peerfd, &one, sizeof(one));
        syslog(LOG_INFO, "User login successful.");
    }else{
        printf("验证失败\n");
        one = 0;
        sendn(task->peerfd, &one, sizeof(one));
    }

}
